package com.lxmes.sales.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.lxmes.base.domain.NcSupplier;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.bean.BeanUtils;
import com.lxmes.sales.domain.CcPurchaseInfoVo;
import com.lxmes.sales.domain.ZcQuotationToOrder;
import com.lxmes.sales.mapper.NcSalesOrderMapper;
import com.lxmes.sales.mapper.ZcQuotationToOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;

import javax.annotation.Resource;
import javax.validation.Validator;
import com.lxmes.sales.mapper.ZcBidNoticeMapper;
import com.lxmes.sales.domain.ZcBidNotice;
import com.lxmes.sales.service.IZcBidNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 中标通知单Service业务层处理
 *
 * @author lx
 * @date 2023-06-21
 */
@Service
public class ZcBidNoticeServiceImpl implements IZcBidNoticeService
{
    @Autowired
    private ZcBidNoticeMapper zcBidNoticeMapper;

    @Autowired
    protected Validator validator;

    @Resource
    private NcSalesOrderMapper ncSalesOrderMapper;


    @Autowired
    private ZcQuotationToOrderMapper zcQuotationToOrderMapper;

    /**
     * 查询中标通知单
     *
     * @param id 中标通知单主键
     * @return 中标通知单
     */
    @Override
    public ZcBidNotice selectZcBidNoticeById(Long id)
    {
        return zcBidNoticeMapper.selectZcBidNoticeById(id);
    }

    /**
     * 查询中标通知单列表
     *
     * @param zcBidNotice 中标通知单
     * @return 中标通知单
     */
    @Override
    public List<ZcBidNotice> selectZcBidNoticeList(ZcBidNotice zcBidNotice)
    {
        return zcBidNoticeMapper.selectZcBidNoticeList(zcBidNotice);
    }

    /**
     * 新增中标通知单
     *
     * @param zcBidNotice 中标通知单
     * @return 结果
     */
    @Override
    public int insertZcBidNotice(ZcBidNotice zcBidNotice)
    {
        return zcBidNoticeMapper.insertZcBidNotice(zcBidNotice);
    }

    /**
     * 修改中标通知单
     *
     * @param zcBidNotice 中标通知单
     * @return 结果
     */
    @Override
    public int updateZcBidNotice(ZcBidNotice zcBidNotice)
    {
        return zcBidNoticeMapper.updateZcBidNotice(zcBidNotice);
    }

    /**
     * 批量删除中标通知单
     *
     * @param ids 需要删除的中标通知单主键
     * @return 结果
     */
    @Override
    public int deleteZcBidNoticeByIds(Long[] ids)
    {
        return zcBidNoticeMapper.deleteZcBidNoticeByIds(ids);
    }

    /**
     * 删除中标通知单信息
     *
     * @param id 中标通知单主键
     * @return 结果
     */
    @Override
    public int deleteZcBidNoticeById(Long id)
    {
        return zcBidNoticeMapper.deleteZcBidNoticeById(id);
    }
    @Override
    public String imporIteminfo(List<ZcBidNotice> zcBidNoticeList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(zcBidNoticeList) || zcBidNoticeList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZcBidNotice zcBidNotice : zcBidNoticeList)
        {
            try
            {
                BeanValidators.validateWithException(validator, zcBidNotice);
                zcBidNotice.setCreateBy(operName);
                zcBidNotice.setUpdateBy(operName);
                this.insertZcBidNotice(zcBidNotice);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional
    public int toOrder(Long id) {
        ZcBidNotice zcBidNotice = selectZcBidNoticeById(id);
        if("1".equals(zcBidNotice.getIsOrder())){
            throw new RuntimeException("该数据已经转为订单！");
        }
        CcPurchaseInfoVo ccPurchaseInfoVo = new CcPurchaseInfoVo();
        ccPurchaseInfoVo.setFactoryName(zcBidNotice.getCompanyName());
        ccPurchaseInfoVo.setFactoryCode(zcBidNotice.getCompanyCode());
        String dh = "CG-"+ NewNumber.createNewNumber(ncSalesOrderMapper.getCaiGouNewCode(DateUtils.getDate()));
        ccPurchaseInfoVo.setDanhao(dh);
        ccPurchaseInfoVo.setCreateTime(DateUtils.getNowDate());
        ccPurchaseInfoVo.setCreateBy(SecurityUtils.getUsername());
        ccPurchaseInfoVo.setYhType("采购计划");
        ccPurchaseInfoVo.setYhDanhao(dh);
        ccPurchaseInfoVo.setAsnDanhao(dh);
        ccPurchaseInfoVo.setItemCode(zcBidNotice.getMaterialNumber());
        ccPurchaseInfoVo.setItemId(zcBidNotice.getMaterialNumber());
        ccPurchaseInfoVo.setItemName(zcBidNotice.getMaterialName());
        ccPurchaseInfoVo.setSpec(zcBidNotice.getSpecification());
//        ccPurchaseInfoVo.setSupplyId(supplier.getId());
        ccPurchaseInfoVo.setSupplyName(zcBidNotice.getQuoteUnit());
        ccPurchaseInfoVo.setYhNum(zcBidNotice.getPurchaseQuantity());
//        ccPurchaseInfoVo.setJhUnitPrice(ncSalesOrder.getItemPrice());
        ncSalesOrderMapper.insertCcPurchaseInfo(ccPurchaseInfoVo);
        ZcQuotationToOrder zcQuotationToOrder = new ZcQuotationToOrder();
        BeanUtils.copyProperties(zcBidNotice,zcQuotationToOrder);
        zcQuotationToOrderMapper.insertZcQuotationToOrder(zcQuotationToOrder);
        zcBidNotice.setIsOrder("1");
        return zcBidNoticeMapper.updateZcBidNotice(zcBidNotice);
    }
}
